import router from './router'
import store from './store'
import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import { getToken } from '@/utils/auth' // get token from cookie
import getPageTitle from '@/utils/get-page-title'

NProgress.configure({ showSpinner: false }) // NProgress Configuration

const whiteList = ['/dashboard', '/login', '/auth-redirect', '/whiteList'] // no redirect whitelist

router.beforeEach(async (to, from, next) => {
  // start progress bar
  NProgress.start()

  // set page title
  document.title = getPageTitle(to.meta.title)

  // determine whether the user has logged in
  const hasToken = getToken()
  if (hasToken && whiteList.indexOf(to.path) === -1) {
    if (to.path === '/login') {
      // if is logged in, redirect to the home page
      next({ path: '/dashboard' })
      NProgress.done()
    } else {
      let uu = window.location.hash.split('?')[1]
      if (uu == 'a=b') {
        if (store.getters.permissions.length === 0) {
          // 判断当前用户是否已拉取完user_info信息
          
          store
          .dispatch('user/getInfo')
          .then((res) => {
            // 拉取user_info
            // 加载数据字典
            store.dispatch('dict/GetDicts')
            // 获取系统配置
              store.dispatch('sysConfig/getConfig')
              
              // 获取路由
              if (store.getters.fetchRoutes.length === 0) {
                // 判断是否获取路由
                store.dispatch('permission/GetRoutes').then(() => {
                  if (store.getters.fetchRoutes.length === 0) {
                    Message.error('你的账号权限存在问题')
                    next('/login') // 否则全部重定向到登录页
                    return
                  }
                  store.getters.addRoutes.forEach((item) => {
                    router.addRoute(item) // 动态添加可访问路由表
                  })
                  // 获取权限
                  store.dispatch('permission/GetPermissions').then(() => {
                    // 获取权限
                    if (store.getters.permissions.length === 0) {
                      Message.error('你的账号权限存在问题')
                      next('/login') // 否则全部重定向到登录页
                    } else {
                      next('/dashboard?a=b')
                      NProgress.done()
                    }
                  })
                })
              }
            })
            .catch(() => {
              store.dispatch('user/logout').then(() => {
                // Message.error('登录超时')
                next('/login') // 否则全部重定向到登录页
              })
            })
          } else {
            next()
        }
      } else {
        if (store.getters.permissions.length === 0) {
          // 判断当前用户是否已拉取完user_info信息
          store
            .dispatch('user/getInfo')
            .then((res) => {
              // 拉取user_info
              // 加载数据字典
              store.dispatch('dict/GetDicts')
              // 获取系统配置
              store.dispatch('sysConfig/getConfig')

              // 获取路由
              if (store.getters.fetchRoutes.length === 0) {
                // 判断是否获取路由
                store.dispatch('permission/GetRoutes').then(() => {
                  if (store.getters.fetchRoutes.length === 0) {
                    Message.error('你的账号权限存在问题')
                    next('/login') // 否则全部重定向到登录页
                    return
                  }
                  store.getters.addRoutes.forEach((item) => {
                    router.addRoute(item) // 动态添加可访问路由表
                  })
                  // 获取权限
                  store.dispatch('permission/GetPermissions').then(() => {
                    // 获取权限
                    if (store.getters.permissions.length === 0) {
                      Message.error('你的账号权限存在问题')
                      next('/login') // 否则全部重定向到登录页
                    } else {
                      next('/dashboard')
                      NProgress.done()
                    }
                  })
                })
              }
            })
            .catch(() => {
              store.dispatch('user/logout').then(() => {
                // Message.error('登录超时')
                next('/login') // 否则全部重定向到登录页
              })
            })
        } else {
          next()
        }
      }
    }
  } else {
    /* has no token*/

    if (whiteList.indexOf(to.path) !== -1) {
      // in the free login whitelist, go directly
      next()
      NProgress.done()
    } else {
      // other pages that do not have permission to access are redirected to the login page.
      next(`/login`)
      NProgress.done()
    }
  }
})

router.afterEach(() => {
  // finish progress bar
  NProgress.done()
})
